﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OperaHouse.Repository.Contacts;
using OperaHouse.DAL.Infrastructure;
using OperaHouse.Domain.Contacts;
using OperaHouse.Common;

namespace OperaHouse.Business.Contacts
{
    public partial class ContactService : IContactService
    {
        #region Fields

        private readonly IContactRepository _contactRepository;
        private readonly IUnitOfWork _unitOfWork;

        #endregion

        #region Ctor

        public ContactService(IContactRepository contactRepository, IUnitOfWork unitOfWork)
        {
            this._contactRepository = contactRepository;
            this._unitOfWork = unitOfWork;
        }

        #endregion

        #region Methods
        public IPagedList<Contact> GetAllContact(int pageIndex, int pageSize)
        {
            var query = _contactRepository.Table;

            query = query.OrderByDescending(u => u.CreatedOn);

            var news = new PagedList<Contact>(query, pageIndex, pageSize);
            return news;
        }

        public Contact GetContactById(int id)
        {
            return _contactRepository.GetById(id);
        }

        public void InsertContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Insert(contact);
            _unitOfWork.Commit();
        }

        public void UpdateContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Update(contact);
            _unitOfWork.Commit();
        }

        public void DeleteContact(Contact contact)
        {
            if (contact == null)
                throw new ArgumentNullException("contact");

            _contactRepository.Delete(contact);
            _unitOfWork.Commit();
        }
        #endregion
    }
}
